/**
 * 获取后端数据
 */
const apiUtil = require("../utils/apiUtil")
const dateUtil = require("../utils/dateUtil")
const marked = require('marked')
const indexApi = {
    /**
     * 获取博文列表
     * @param {*} currentPage 当前页面
     * @param {*} pageSize  页面大小
     * @returns Promise
     */
    articleList(currentPage = 1, pageSize = 5) {
        return new Promise((resolve, reject) => {
            apiUtil.get("/blog/listAll", { currentPage, pageSize }).then(resp => {
                if (resp.code == 200) {
                    resp.data.records.forEach(item => {
                        if (!String(item.cover).startsWith("http")) {
                            item.cover = apiUtil.defaultApiPath + item.cover
                        }
                        item.created = dateUtil.dateFormat("YY-mm-dd", new Date(item.created))
                    });
                    resolve(resp.data)
                } else {
                    reject(resp.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    },
    /**
     * 获取博文列表（简单描述）
     * @param {*} currentPage 当前页面
     * @param {*} pageSize  页面大小
     * @returns Promise
     */
    simpleArticleList(currentPage = 1, pageSize = 10) {
        return new Promise((resolve, reject) => {
            apiUtil.get("/blog/list", { currentPage, pageSize }).then(resp => {
                if (resp.code == 200) {
                    resolve(resp.data)
                } else {
                    reject(resp.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    },
    getArticleById(id) {
        return new Promise((resolve, reject) => {
            apiUtil.get("/blog/blogDetails/" + id).then(resp => {
                if (resp.code == 200) {
                    resp.data.created = dateUtil.dateFormat("YY-mm-dd", new Date(resp.data.created))
                    resp.data.content = marked(resp.data.content)
                    resolve(resp.data)
                } else {
                    reject(resp.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    },
}
module.exports = indexApi;